package ch.fhnw.qualiscan.application.modals;

import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Button.ClickEvent;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.application.views.AdminQuestionnaireView;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class AdminDeleteQuestion extends Modal {
	
	private Question question;
	private AdminQuestionnaireView caller;
	private AdminModel adminModel;
	private boolean subquestion;
	
	public AdminDeleteQuestion(Question question, AdminQuestionnaireView caller, boolean subquestion) {
		this.question = question;
		this.caller = caller;
		this.subquestion = subquestion;
		
		adminModel = new AdminModel();
		buildLayout();
	}
	
	public void buildLayout() {
		setCaption(TM.get("admin.deletequestion.title"));
		
		addComponent(textHelper.p(TM.get("admin.deletequestion.desc")));
		HorizontalLayout buttonBar = new HorizontalLayout();
		buttonBar.setSpacing(true);
		buttonBar.setMargin(true, false, false, false);
		
		addComponent(buttonBar);
		
		Button confirmButton = new Button(TM.get("action.confirm"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				adminModel.deleteQuestion(question, subquestion, caller.getCurrentDomain());
				caller.displayQuestions(caller.getDomainSelect().getValue());
				
				AdminDeleteQuestion add = (AdminDeleteQuestion) event.getButton().getData();
				add.close();
			}
		});
		confirmButton.setIcon(Config.ICON_ACCEPT);
		confirmButton.setData(this);
		
		Button cancelButton = new Button(TM.get("action.cancel"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				AdminDeleteQuestion add = (AdminDeleteQuestion) event.getButton().getData();
				add.close();
			}
		});
		cancelButton.setIcon(Config.ICON_CANCEL);
		cancelButton.setData(this);
		
		buttonBar.addComponent(confirmButton);
		buttonBar.addComponent(cancelButton);
	}
	
	
	
}
